package com.yixun.qfbao.controller;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.security.GeneralSecurityException;
import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.validation.Valid;

import com.qiaofang.common.util.http.HttpClientUtil;
import com.yixun.qfbao.util.RequestUtil;
import com.yixun.qfbao.util.SMSUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.yixun.qfbao.annotations.History;
import com.yixun.qfbao.api.service.CompanyService;
import com.yixun.qfbao.api.service.HomeService;
import com.yixun.qfbao.api.service.OrganizationService;
import com.yixun.qfbao.api.service.PersonnelService;
import com.yixun.qfbao.api.service.login.LoginFactory;
import com.yixun.qfbao.apidto.AddAgentDeptReqDto;
import com.yixun.qfbao.apidto.AddAgentDeptResDto;
import com.yixun.qfbao.apidto.AddPartnerReqDto;
import com.yixun.qfbao.apidto.AddPartnerResDto;
import com.yixun.qfbao.apidto.GetCityReqDto;
import com.yixun.qfbao.apidto.GetParentDeptReqDto;
import com.yixun.qfbao.apidto.GetParentDeptResDto;
import com.yixun.qfbao.apidto.ModelResDto;
import com.yixun.qfbao.apidto.base.CommonReqDTO;
import com.yixun.qfbao.config.H5UrlConfig;
import com.yixun.qfbao.constant.RedisKeyConstant;
import com.yixun.qfbao.mapper.CompanyMapper;
import com.yixun.qfbao.model.Area;
import com.yixun.qfbao.model.Company;
import com.yixun.qfbao.model.Department;
import com.yixun.qfbao.model.Model;
import com.yixun.qfbao.model.ModelInfo;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.model.Subscription;
import com.yixun.qfbao.model.ThreadLocalContainer;
import com.yixun.qfbao.model.dto.AreaDto;
import com.yixun.qfbao.model.dto.CompanyDto;
import com.yixun.qfbao.model.enums.CheckStatusEnum;
import com.yixun.qfbao.model.enums.DelFlagEnum;
import com.yixun.qfbao.model.enums.MechanismAccountTypeEnum;
import com.yixun.qfbao.model.enums.ModelTypeEnum2;
import com.yixun.qfbao.model.enums.SystemIdEnum;
import com.yixun.qfbao.request.BaseRequest;
import com.yixun.qfbao.request.GenericIdRequest;
import com.yixun.qfbao.request.GenericStringRequest;
import com.yixun.qfbao.request.login.AgentCompanyRegesitReqDto;
import com.yixun.qfbao.request.login.CompanyIdReqDto;
import com.yixun.qfbao.request.login.ModelReqDto;
import com.yixun.qfbao.request.login.NewLoginReqDto;
import com.yixun.qfbao.request.login.RegistReqDto;
import com.yixun.qfbao.request.login.ResetPwdReqDto;
import com.yixun.qfbao.request.login.VerificationCodeReqDto;
import com.yixun.qfbao.response.BaseResponse;
import com.yixun.qfbao.response.CommonResponse;
import com.yixun.qfbao.response.GenericLongResponse;
import com.yixun.qfbao.response.home.AttetionResDto;
import com.yixun.qfbao.response.login.AreaListResponse;
import com.yixun.qfbao.response.login.AreaResponse;
import com.yixun.qfbao.response.login.CompanyRegesitInfoResponse;
import com.yixun.qfbao.response.login.PartnerInfoListResponse;
import com.yixun.qfbao.response.login.PartnerInfoResponse;
import com.yixun.qfbao.response.login.PartnerRegesiterModelResponse;
import com.yixun.qfbao.response.login.StoreAndGroupListResponse;
import com.yixun.qfbao.response.login.StoreAndGroupResponse;
import com.yixun.qfbao.service.AreaService;
import com.yixun.qfbao.service.ModelInfoService;
import com.yixun.qfbao.service.ModelService;
import com.yixun.qfbao.service.PartnerInfoService;
import com.yixun.qfbao.util.RedisUtil;
import com.yixun.qfbao.util.StringUtil;
import com.yixun.qfbao.util.exception.TServerException;
import com.yixun.qfbao.utils.LoginUtils;
import com.yixun.qfbao.utils.WeAppUtils;
import com.yixun.qfbao.xf.model.AttetionInfo;
import com.yixun.qfbao.xf.model.enums.PartnerStatusEnum;
import com.yixun.qfbao.xf.model.enums.ResultEnum;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import static com.qiaofang.common.util.http.HttpClientUtil.*;

/**
 * @author zilong.liu
 * @date 2020-7-30
 * @desc 
 *		登录接口使用的controller
 *
 */
@Api(tags = {"登录"})
@Controller
@ResponseBody
@RequestMapping("/api/newlogin")
public class LoginController {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(LoginController.class);
	
	@Autowired
	PersonnelService personnelService;
	
	@Autowired
	LoginUtils loginUtils;
	
	@Autowired
	PartnerInfoService partnerInfoService;
	
	@Autowired
	CompanyService companyService;
	
	@Autowired
	CompanyMapper companyMapper;
	
	@Autowired
    OrganizationService organizationService;
	
	@Autowired
	AreaService areaService;
	
	@Autowired
	WeAppUtils weAppUtils;
	
	@Autowired
	ModelService modelService;
	
	@Autowired
	HomeService homeService;
	
	@Autowired
	com.yixun.qfbao.api.service.AreaService areaService2;
	
	@Autowired 
	ModelInfoService modelInfoService;
	
	@Autowired
	H5UrlConfig h5UrlConfig;

	@Autowired
	private SMSUtil smsUtil;
	
	private static final String AGENCY_WEBURL = "?companyUUid=COMPANYUUID#/login";
	private static final String COMPANY_UUID = "COMPANYUUID";
	
	/**
	 *  获取短信验证码
	 *  	注册时需要传入公司ID，获取验证码，判断当前人员是否存在
	 * @param resDto
	 * @return
	 */
	@ApiOperation(value="获取短信验证码", response = CommonResponse.class)
	@PostMapping("/getVerificationCode")
 	public CommonResponse getVerificationCode(@RequestBody @Valid BaseRequest<VerificationCodeReqDto> reqDto) {
		VerificationCodeReqDto verificationCodeReqDto = reqDto.getData();
		String phone = verificationCodeReqDto.getPhone();
		String type = verificationCodeReqDto.getType();
		Long companyId = verificationCodeReqDto.getCompanyId();
		List<String> companyUuids = reqDto.getCompanyUuids();
		
		String loginType = reqDto.getLoginType();
		Random random = new Random();
		String fourRandom = random.nextInt(10000) + "";
		int randLength = fourRandom.length();
		if(randLength<6){
			for(int i=1; i<=6-randLength; i++)
				fourRandom = "0" + fourRandom ;
		}
		ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 1000, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
		String  postUrl = "http://115.29.170.211:8085/sms.aspx";
		String finalFourRandom = fourRandom;
		CommonResponse commonResponse = new CommonResponse();
		executor.execute(new Runnable() {
			@Override
			public void run() {
				try {
					//逻辑
					Map<String, String> params = new HashMap<>();
					params.put("userid","4562");
					params.put("account","hjxx-qdw");
					params.put("password","hjxx2021Qdw");
					params.put("action","send");
					params.put("mobile",phone);
					params.put("content","验证码为:"+ finalFourRandom +",有效期3分钟，请勿泄露，他人索取行为均可能为诈骗。【渠道王平台】");
					params.put("extno","01");
					params.put("sendTime","");
					String result = null;
					try {
						result = RequestUtil.buildRequest(postUrl, params, "UTF-8");
					} catch (GeneralSecurityException e) {
						e.printStackTrace();
					}
					//String  resultMsg = HttpClientUtil.class.(postUrl,params);
					//smsUtil.sendMessage()
					System.out.println("手机号"+phone+"短信验证码发生结果是："+result);
					//将短信验证码存到Redis
					RedisUtil.set(phone+"-"+loginType+"-"+type+"",finalFourRandom);
					commonResponse.setCode(ResultEnum.SUCCESS.getNo());
					commonResponse.setMsg(result);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
		return commonResponse;
	}


	@ApiOperation(value="获取短信验证码", response = CommonResponse.class)
	@PostMapping("/getVerificationCodeEx")
	public CommonResponse getVerificationCodeEx(@RequestBody @Valid BaseRequest<VerificationCodeReqDto> reqDto) {
		VerificationCodeReqDto verificationCodeReqDto = reqDto.getData();
		String phone = verificationCodeReqDto.getPhone();
		Random random = new Random();
		String fourRandom = random.nextInt(10000) + "";
		int randLength = fourRandom.length();
		if(randLength<6){
			for(int i=1; i<=6-randLength; i++)
				fourRandom = "0" + fourRandom ;
		}
		ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 1000, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
		String  postUrl = "http://115.29.170.211:8085/sms.aspx";
		String finalFourRandom = fourRandom;
        CommonResponse commonResponse = new CommonResponse();
		executor.execute(new Runnable() {
			@Override
			public void run() {
				try {
					//逻辑
					Map<String, String> params = new HashMap<>();
					params.put("userid","4562");
					params.put("account","hjxx-qdw");
					params.put("password","hjxx2021Qdw");
					params.put("action","send");
					params.put("mobile",phone);
					params.put("content","验证码为:"+ finalFourRandom +",有效期3分钟，请勿泄露，他人索取行为均可能为诈骗。【渠道王平台】");
					params.put("extno","01");
					params.put("sendTime","");
					String result = null;
					try {
						result = RequestUtil.buildRequest(postUrl, params, "UTF-8");
					} catch (GeneralSecurityException e) {
						e.printStackTrace();
					}
					//String  resultMsg = HttpClientUtil.class.(postUrl,params);
					//smsUtil.sendMessage()
					System.out.println("手机号"+phone+"短信验证码发生结果是："+result);
					//将短信验证码存到Redis
					Date nowTime = new Date();
				//	RedisUtil.set("+phone+",",finalFourRandom+","+nowTime.getTime());
                    commonResponse.setCode(ResultEnum.SUCCESS.getNo());
                    commonResponse.setMsg(result);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
		return commonResponse;
	}


	//根据公司的ID获取对应的机构和组别
	@SuppressWarnings("unchecked")
	@ApiOperation(value="根据公司的id获取对应的机构和组别", response = StoreAndGroupListResponse.class)
	@PostMapping("/getOrgAndGroupByCompanyId")
	public BaseResponse<StoreAndGroupListResponse> getOrgAndGroupByCompanyId(@RequestBody @Valid BaseRequest<CompanyIdReqDto> resDto) {
		BaseResponse<StoreAndGroupListResponse> res = new BaseResponse<>();
		CompanyIdReqDto companyIdReqDto = resDto.getData();
		long companyId = companyIdReqDto.getCompanyId();
		
		String companyCode = companyId == 0L? companyIdReqDto.getCompanyCode(): null;
		List<String> companyUuids = resDto.getCompanyUuids();
		Company company = companyService.selectCompanyByCompanyCode(companyCode, 
				companyId == 0L? null: companyId, companyUuids);
		if(company == null) {
			res.setCode(ResultEnum.FAIL.getNo());
			res.setMsg("未查询到对应的公司");
			return res;
		}
		companyCode = company.getCompanyCode();
		companyId = company.getId();
		long storeId = companyIdReqDto.getStoreId();
		long groupId = companyIdReqDto.getGroupId();
		String loginType = resDto.getLoginType();
		
		List<Map<String, Object>> storeAndGroupList = 
				LoginFactory.getLoginService(loginType).getOrgAndGroupByCompanyUuid(companyId, storeId, groupId);
		
		res.setCode(ResultEnum.SUCCESS.getNo());
		if(storeAndGroupList == null) {
			res.setCode(ResultEnum.SUCCESS.getNo());
			res.setMsg("无组别和机构信息");
			return res;
		}
		StoreAndGroupListResponse storeAndGroupListResponse = new StoreAndGroupListResponse();
		//设置外层出参
		storeAndGroupListResponse.setCompanyCode(companyCode);
		storeAndGroupListResponse.setCompanyId(companyId);
		
		//设置部门和机构的数组
		for (int i = 0, j = storeAndGroupList.size(); i < j; i ++) {
			if(i != j - 1) {
				StoreAndGroupResponse storeAndGroupResponse = new StoreAndGroupResponse();
				Department store = (Department)storeAndGroupList.get(i).get("storeInfo");
				store.setSubDepartment((List<Department>)storeAndGroupList.get(i).get("groupList"));
				storeAndGroupResponse.setStoreInfo(store);
				storeAndGroupListResponse.addStoreAndGroupResponse(storeAndGroupResponse);
			}else {
				Object storeObj = storeAndGroupList.get(i).get("storeName"); 
				Object groupObj = storeAndGroupList.get(i).get("groupName"); 
				storeAndGroupListResponse.setStoreName(
						storeObj == null ? "": String.valueOf(storeObj));
				storeAndGroupListResponse.setGroupName(
						groupObj == null ? "": String.valueOf(groupObj));
			}
		}
		res.setData(storeAndGroupListResponse);
		res.setMsg("查询成功");
		return res;
	}
	
	/**
	 * 	获取注册模板
	 * @param resDto
	 */
	@ApiOperation(value="获取注册模板", response = PartnerRegesiterModelResponse.class)
	@PostMapping("/getPartnerRegesiterModel")
	public BaseResponse<PartnerRegesiterModelResponse> getPartnerRegesiterModel(@RequestBody @Valid BaseRequest<GenericIdRequest> req) {
		BaseResponse<PartnerRegesiterModelResponse> res = new BaseResponse<>();
		
		CompanyDto entity = new CompanyDto();
		entity.setId(req.getData().getId());
		Company company = companyService.selectByDto(entity);
		if(company == null) {
			res.setCode(ResultEnum.FAIL.getNo());
			res.setMsg("机构邀请码错误");
			return res;
		}
		
		Model model = new Model();
		model.setCompanyId(req.getData().getId());
		model.setType(ModelTypeEnum2.PARTNER_CHECK.getNo());
		List<Model> partnerCheckModelList = modelService.selectListByDto(model);
		
		model.setType(ModelTypeEnum2.COMPANY.getNo());
		List<Model> companyRegesitCheckModelList = modelService.selectListByDto(model);
		
		PartnerRegesiterModelResponse partnerRegesiterModelResponse = new PartnerRegesiterModelResponse();
		partnerRegesiterModelResponse.setPartnerCheckModelResDtoList(ModelResDto.listConvertModelToModelResDto(partnerCheckModelList));
		partnerRegesiterModelResponse.setCompanyRegesitModelResDtoList(ModelResDto.listConvertModelToModelResDto(companyRegesitCheckModelList));
		res.setCode(ResultEnum.SUCCESS.getNo());
		res.setData(partnerRegesiterModelResponse);
		return res;
	}
	
	/**
	 * 修改密码
	 * @param resDto
	 * @return
	 */
	@ApiOperation(value="修改密码", response = CommonResponse.class)
	@PostMapping("/updatePwd")
	public CommonResponse updatePwd(@RequestBody @Valid BaseRequest<ResetPwdReqDto> resDto) {
		CommonResponse commonResponse = new CommonResponse();
		ResetPwdReqDto resetPwdReqDto = resDto.getData();
		String phone = resetPwdReqDto.getPhone();
		String verificationCode = resetPwdReqDto.getVerificationCode();
		String pwd = resetPwdReqDto.getPwd();
		String loginType = resDto.getLoginType();
		String result = LoginFactory.getLoginService(loginType).resetPwd(phone, pwd, verificationCode, loginType);
		commonResponse.setCode("成功".equals(result)? ResultEnum.SUCCESS.getNo(): ResultEnum.FAIL.getNo());
		commonResponse.setMsg(result);
		return commonResponse;
	}
	
	/**
	 * 账户注册
	 *	1、 先验证验证码
	 *	2、新增用户。调用原注册方法
	 * @param resDto
	 * @return
	 * @throws Exception 
	 */
	@ApiOperation(value="账户注册", response = CommonResponse.class)
	@PostMapping("/regist")
	public BaseResponse<AttetionResDto> regist(@RequestBody @Valid BaseRequest<RegistReqDto> resDto) throws Exception {
		String loginType = resDto.getLoginType();
		RegistReqDto registReqDto = resDto.getData();
		String accountName = registReqDto.getAccountName();
		String companyCode = registReqDto.getCompanyCode();
		String groupId = registReqDto.getGroupId();
		String storeId = registReqDto.getStoreId();
		String phone = registReqDto.getPhone();
		String verificationCode = registReqDto.getVerificationCode();
		String pwd = registReqDto.getPwd();
		Long qrcodeOriginPartnerId = registReqDto.getQrcodeOriginPartnerId();
		List<String> companyUuids = resDto.getCompanyUuids();
		BaseResponse<AttetionResDto> response = new BaseResponse<>();
		Company company = companyService.selectCompanyByCompanyCode(companyCode, null, companyUuids);
		// 此处是分销公司
		if(company == null) {
			response.setCode(ResultEnum.FAIL.getNo());
			response.setMsg("未查询到对应的公司");
			return response;
		}
		
		// 拼装模板入参
		List<ModelReqDto> modelReqDtoList = registReqDto.getModelReqDtoList();
		List<ModelInfo> modelInfoList = buildModelInfo(modelReqDtoList, ModelTypeEnum2.PARTNER_CHECK.getNo());
		ThreadLocalContainer.Regesiter.MODEL_INFO_LIST.set(modelInfoList);
		
		// 获取当前人员的小程序openId信息，并传递至后台
		String jsCode = registReqDto.getJsCode();
		
		
		//校验
		String result = LoginFactory.getLoginService(loginType).regist(accountName, companyCode, groupId, storeId,
				phone, verificationCode, pwd, loginType, qrcodeOriginPartnerId);
		
		if((result.indexOf("成功") > -1)) {
			Long userId = ThreadLocalContainer.Regesiter.USER_ID.get();
			String companyUuid = ThreadLocalContainer.Regesiter.COMPANY_UUID.get();
			// 1、设置openId
			if(StringUtil.isNotNull(jsCode)) {
				setOpenId(jsCode, loginType, companyUuid, String.valueOf(userId), "1");
			}
			
			// 2、 获取关注信息
			AttetionInfo attetionInfo = homeService.getAttetionInfo(loginType, String.valueOf(userId), companyUuid);
			AttetionResDto attetionResDto = new AttetionResDto();
			response.setData(attetionResDto);
			BeanUtils.copyProperties(attetionResDto, attetionInfo);
		}
		
		
		// 清除threadlocal变量信息
		clearRegesiterThreadLocal();
		
		response.setMsg(result);
		response.setCode((result.indexOf("成功") > -1)? ResultEnum.SUCCESS.getNo(): ResultEnum.FAIL.getNo());
		return response;
	}
	
	/**
	 * 	清除threadlocal变量信息
	 */
	private void clearRegesiterThreadLocal() {
		ThreadLocalContainer.Regesiter.MODEL_INFO_LIST.remove();
//		ThreadLocalContainer.Regesiter.OPEN_ID.remove();
		ThreadLocalContainer.Regesiter.USER_ID.remove();
		ThreadLocalContainer.Regesiter.COMPANY_UUID.remove();
	}
	
	/**
	 * @param modelReqDtoList
	 */
	private List<ModelInfo> buildModelInfo(List<ModelReqDto> modelReqDtoList, String type) {
		if(!CollectionUtils.isEmpty(modelReqDtoList)) {
			List<ModelInfo> modelInfoList = new ArrayList<>();
			for (ModelReqDto modelReqDto : modelReqDtoList) {
				ModelInfo modelInfo = new ModelInfo();
				modelInfo.setModelId(modelReqDto.getId());
				modelInfo.setModelType(type);
				modelInfo.setModelName(modelReqDto.getName());
				modelInfo.setModelValue(modelReqDto.getValue());
				modelInfoList.add(modelInfo);
			}
			return modelInfoList;
		}
		return null;
	}
	
	/**
	 * 1、如果是验证码登录，则验证验证码
	 * 2、查找到对应的partner
	 * 3、将partner信息存入登录的redis中
	 * 4、如果前台传递了jscode，则请求到对应的openId，并返回到前台
	 * 
	 * 	历史只能一个人登录，为了避免小程序缓存的问题。可删除
	 * 
	 * 	为了避免微信小程序的缓存，部分请求老接口，因此暂时保留
	 * @param reqDto
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@ApiOperation(value="用户登录", response = PartnerInfoResponse.class)
	@PostMapping("/login")
	@History(sinceTime="2020-12-01 00:00:00")
	public BaseResponse<PartnerInfoResponse> login(@RequestBody @Valid BaseRequest<NewLoginReqDto> reqDto) {
		NewLoginReqDto loginReqDto = reqDto.getData();
		String phone = loginReqDto.getPhone();
		String verificationCode = loginReqDto.getVerificationCode();
		String pwd = loginReqDto.getPwd();
		String openId = loginReqDto.getOpenId();
		String jsCode = loginReqDto.getJsCode();
		int loginWay = loginReqDto.getLoginWay();
		String loginType = reqDto.getLoginType();
		List<String> companyUuids = reqDto.getCompanyUuids();
		
		BaseResponse<PartnerInfoResponse> res = new BaseResponse<>();
		if(loginWay == 2) {
			if(StringUtils.isBlank(pwd) || pwd.length() < 6 || pwd.length() > 20) {
				res.setMsg("密码必须为6-20位");
				res.setCode(ResultEnum.FAIL.getNo());
				return res;
			}
		}
		
		Map<String, Object> resMap = LoginFactory.getLoginService(loginType).login(phone, 
				verificationCode, pwd, loginWay, loginType, openId, companyUuids);
		LOGGER.info("用户登录查询返回resMap:{}", resMap);
		
		String resultMsg = String.valueOf(resMap.get("msg"));
		if(!"成功".equals(resultMsg)) {
			res.setMsg(resultMsg);
			res.setCode(ResultEnum.FAIL.getNo());
			return res;
		}
		Map<String, Object> partnerInfo = (Map<String, Object>) resMap.get("partnerInfo");
		String partnerId = String.valueOf(partnerInfo.get("partnerId"));
		String accountName = String.valueOf(partnerInfo.get("accountName"));
		String province = String.valueOf(partnerInfo.get("province"));
		String city = String.valueOf(partnerInfo.get("city"));
		String zone = String.valueOf(partnerInfo.get("zone"));
		String agentCompanyUuid = String.valueOf(partnerInfo.get("agent_companyUuid"));
		String agentCompanyId = String.valueOf(partnerInfo.get("id"));
		String assistantCompanyId = String.valueOf(partnerInfo.get("companyId"));
		String assistantCompanyUuid = String.valueOf(partnerInfo.get("companyUuid"));
		String agentCompanyName = String.valueOf(partnerInfo.get("agent_company_name"));
		String deptId = String.valueOf(partnerInfo.get("dept_id"));
		String userRoleId = String.valueOf(partnerInfo.get("user_roleId"));
		String enableWeappMsg = String.valueOf(partnerInfo.get("enable_weapp_msg"));
		String accountType = String.valueOf(partnerInfo.get("accountType"));
		String isAdmin = String.valueOf(partnerInfo.get("isAdmin"));
		
		PartnerInfo pi = new PartnerInfo();//保存至redis的数据
		// 没有放置wxopenId 以及 weappOpenId进去
		PartnerInfoResponse partnerInfoResponse = new PartnerInfoResponse();//返回数据
		pi.setAccountName(String.valueOf(partnerInfo.get("partnerId")));
		pi.setPartnerInfoId(partnerId);
		pi.setHigherid(Long.parseLong(agentCompanyId));
		pi.setAccountType(accountType);
		pi.setLoginType(loginType);
		pi.setIsAdmin(isAdmin);
		if(StringUtils.isNotBlank(userRoleId) && !"null".equals(userRoleId)) {
			pi.setUserRoleId(Long.parseLong(userRoleId));
		}
		if(StringUtils.isNotBlank(province) && !"null".equals(province)) {
			pi.setProvince(Long.parseLong(province));
		}
		if(StringUtils.isNotBlank(city) && !"null".equals(city)) {
			pi.setCity(Long.parseLong(city));
			AreaDto areaDto = new AreaDto();
			areaDto.setId(Long.parseLong(city));
			Area area = areaService.selectByDto(areaDto);
			partnerInfoResponse.setCityName(area.getName());
		}
		pi.setCompanyId(agentCompanyId);
		pi.setId(Long.parseLong(partnerId));
		loginUtils.putUser(pi);
		// 获取不到weapp_openID 则暂时不处理
		if (SystemIdEnum.WEAPP_AGENCY.getCode().equals(reqDto.getLoginType()) && StringUtils.isNotBlank(jsCode)) {
			try {
				// 如果总代公司未开启微信小程序推送功能，则只更新weapp_openId字段
				JSONObject wxBindInfoJson = getUserOpenIdInfo(jsCode, reqDto.getLoginType(), assistantCompanyUuid);
				if(wxBindInfoJson != null) {
					//返回的是公众号对应的wxopenId
					String serviceWxOpenId = LoginFactory.getLoginService(loginType).updateOpenIdInfo(wxBindInfoJson, partnerId, assistantCompanyUuid, enableWeappMsg);
					partnerInfoResponse.setOpenId(serviceWxOpenId);
				}
			} catch (Exception e) {
				LOGGER.error("login error: {}", e);
			}
		}
		partnerInfoResponse.setPartnerId(partnerId);
		partnerInfoResponse.setAccountName(accountName);
		partnerInfoResponse.setProvince(province==null? "": province);
		partnerInfoResponse.setCity(city==null? "": city);
		partnerInfoResponse.setZone(zone==null? "": zone);
		partnerInfoResponse.setCompanyUuid(assistantCompanyUuid);
		partnerInfoResponse.setCompanyId(Long.parseLong(assistantCompanyId));
		partnerInfoResponse.setAgentComapnyId(Long.parseLong(agentCompanyId));
		partnerInfoResponse.setAgentCompanyUuid(agentCompanyUuid);
		partnerInfoResponse.setAgentCompanyName(agentCompanyName);
		
		GetParentDeptReqDto getParentDeptReqDto = new GetParentDeptReqDto();
		if(StringUtils.isNotBlank(deptId) && !"0".equals(deptId)) {
			getParentDeptReqDto.setDeptId(Long.parseLong(deptId));
			getParentDeptReqDto.setMerchantId(Long.parseLong(agentCompanyId));
			GetParentDeptResDto getParentDeptResDto = organizationService.getParentDeptBySubDept(getParentDeptReqDto);
			if(getParentDeptResDto != null) {
				partnerInfoResponse.setAgentStoreId(String.valueOf(getParentDeptResDto.getStoreId()));
				partnerInfoResponse.setAgentGroupId(String.valueOf(getParentDeptResDto.getGroupId()));
			}
		}
		res.setCode(ResultEnum.SUCCESS.getNo());
		res.setData(partnerInfoResponse);
		return res;
	}
	
	
	
	/**
	 * 1、如果是验证码登录，则验证验证码
	 * 2、查找到对应的partner
	 * 3、将partner信息存入登录的redis中
	 * 4、如果前台传递了jscode，则请求到对应的openId，并返回到前台
	 * @param reqDto
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@ApiOperation(value="用户登录", response = PartnerInfoResponse.class)
	@PostMapping("/newLogin")
	public BaseResponse<PartnerInfoListResponse> newLogin(@RequestBody @Valid BaseRequest<NewLoginReqDto> reqDto) {
		NewLoginReqDto loginReqDto = reqDto.getData();
		String phone = loginReqDto.getPhone();
		String verificationCode = loginReqDto.getVerificationCode();
		String pwd = loginReqDto.getPwd();
		String openId = loginReqDto.getOpenId();
		int loginWay = loginReqDto.getLoginWay();
		String loginType = reqDto.getLoginType();
		List<String> companyUuids = reqDto.getCompanyUuids();
		
		BaseResponse<PartnerInfoListResponse> res = new BaseResponse<>();
		if(loginWay == 2) {
			if(StringUtils.isBlank(pwd) || pwd.length() < 6 || pwd.length() > 20) {
				res.setMsg("密码必须为6-20位");
				res.setCode(ResultEnum.FAIL.getNo());
				return res;
			}
		}
		
		Map<String, Object> resMap = LoginFactory.getLoginService(loginType).newLogin(phone, 
				verificationCode, pwd, loginWay, loginType, openId, companyUuids);
		
		LOGGER.info("用户登录查询返回resMap:{}", resMap);
		String resultMsg = String.valueOf(resMap.get("msg"));
		if(!"成功".equals(resultMsg)) {
			res.setMsg(resultMsg);
			res.setCode(ResultEnum.FAIL.getNo());
			return res;
		}
		List<Map<String, Object>> partnerInfos = (List<Map<String, Object>>) resMap.get("partnerInfos");
		PartnerInfoListResponse partnerInfoListResponse = new PartnerInfoListResponse();
		partnerInfoListResponse.setPartnerInfoListResponse(buildLoginResponse(reqDto, partnerInfos));
		String returnOpenId = setOpenIdForLogin(partnerInfoListResponse.getPartnerInfoListResponse().get(0), reqDto);
		partnerInfoListResponse.getPartnerInfoListResponse().stream().forEach(partnerInfoResponse-> partnerInfoResponse.setOpenId(returnOpenId));
		
		res.setCode(ResultEnum.SUCCESS.getNo());
		res.setData(partnerInfoListResponse);
		return res;
	}
	
	/**
	 * 更新当前人员的openId
	 * @param partnerInfoResponse
	 * @param reqDto
	 * @return
	 */
	private String setOpenIdForLogin(PartnerInfoResponse partnerInfoResponse, BaseRequest<NewLoginReqDto> reqDto) {
		NewLoginReqDto loginReqDto = reqDto.getData();
		String jsCode = loginReqDto.getJsCode();
		String loginType = reqDto.getLoginType();
		return setOpenId(jsCode, loginType, partnerInfoResponse.getCompanyUuid(), partnerInfoResponse.getPartnerId(), partnerInfoResponse.getEnableWeappMsg());
	}
	
	/**
	 * 更新当前人员的openId
	 * @param partnerInfoResponse
	 * @param reqDto
	 * @return
	 */
	private String setOpenId(String jsCode, String loginType, String companyUuid, String userId, String enableWeappMsg) {
		try {
			// 如果总代公司未开启微信小程序推送功能，则只更新weapp_openId字段
			JSONObject wxBindInfoJson = getUserOpenIdInfo(jsCode, loginType, companyUuid);
			if(wxBindInfoJson != null) {
				//返回的是公众号对应的wxopenId
				String serviceWxOpenId = LoginFactory.getLoginService(loginType).updateOpenIdInfo(wxBindInfoJson, 
						userId, companyUuid, enableWeappMsg);
				return serviceWxOpenId;
			}
		} catch (Exception e) {
			LOGGER.error("login error: {}", e);
		}
		return null;
	}
	
	/**
	 * 构建出参
	 * @param reqDto
	 * @param partnerInfos
	 * @return
	 */
	public List<PartnerInfoResponse> buildLoginResponse(@SuppressWarnings("rawtypes") BaseRequest reqDto, List<Map<String, Object>> partnerInfos) {
		List<PartnerInfoResponse> partnerInfoResponses = new ArrayList<>();
		for (Map<String, Object> partnerInfo : partnerInfos) {
			partnerInfoResponses.add(buildLoginPartner(reqDto, partnerInfo));
		}
		Collections.sort(partnerInfoResponses);
		return partnerInfoResponses;
	}
	
	/**
	 * 构建出单的单个partner信息
	 * @param reqDto
	 * @param partnerInfo
	 * @return
	 */
	private PartnerInfoResponse buildLoginPartner(@SuppressWarnings("rawtypes") BaseRequest reqDto, Map<String, Object> partnerInfo) {
		String loginType = reqDto.getLoginType();
		String partnerId = String.valueOf(partnerInfo.get("partnerId"));
		String accountName = String.valueOf(partnerInfo.get("accountName"));
		String province = String.valueOf(partnerInfo.get("province"));
		String city = String.valueOf(partnerInfo.get("city"));
		String zone = String.valueOf(partnerInfo.get("zone"));
		String agentCompanyUuid = String.valueOf(partnerInfo.get("agent_companyUuid"));
		String agentCompanyId = String.valueOf(partnerInfo.get("id"));
		String assistantCompanyId = String.valueOf(partnerInfo.get("companyId"));
		String assistantCompanyUuid = String.valueOf(partnerInfo.get("companyUuid"));
		String agentCompanyName = String.valueOf(partnerInfo.get("agent_company_name"));
		String deptId = String.valueOf(partnerInfo.get("dept_id"));
		String userRoleId = String.valueOf(partnerInfo.get("user_roleId"));
		String accountType = String.valueOf(partnerInfo.get("accountType"));
		String isAdmin = String.valueOf(partnerInfo.get("isAdmin"));
		String enableWeappMsg = String.valueOf(partnerInfo.get("enable_weapp_msg"));
		String companyName = String.valueOf(partnerInfo.get("companyName"));
		String companyAbbreviation = String.valueOf(partnerInfo.get("companyAbbreviation"));
		String checkStatus = String.valueOf(partnerInfo.get("check_status"));
		Date createTime = (Date) partnerInfo.get("createTime");
		
		PartnerInfo pi = new PartnerInfo();//保存至redis的数据
		PartnerInfoResponse partnerInfoResponse = new PartnerInfoResponse();//返回数据
		pi.setAccountName(String.valueOf(partnerInfo.get("partnerId")));
		pi.setPartnerInfoId(partnerId);
		pi.setHigherid(Long.parseLong(agentCompanyId));
		pi.setAccountType(accountType);
		pi.setLoginType(loginType);
		pi.setIsAdmin(isAdmin);
		if(StringUtils.isNotBlank(userRoleId) && !"null".equals(userRoleId)) {
			pi.setUserRoleId(Long.parseLong(userRoleId));
		}
		if(StringUtils.isNotBlank(province) && !"null".equals(province)) {
			pi.setProvince(Long.parseLong(province));
		}
		if(StringUtils.isNotBlank(city) && !"null".equals(city)) {
			pi.setCity(Long.parseLong(city));
			AreaDto areaDto = new AreaDto();
			areaDto.setId(Long.parseLong(city));
			Area area = areaService.selectByDto(areaDto);
			partnerInfoResponse.setCityName(area.getName());
		}
		pi.setCompanyId(agentCompanyId);
		pi.setId(Long.parseLong(partnerId));
		loginUtils.putUser(pi);
		
		partnerInfoResponse.setCheckStatus(checkStatus);
		partnerInfoResponse.setPartnerId(partnerId);
		partnerInfoResponse.setAccountName(accountName);
		partnerInfoResponse.setProvince(province==null? "": province);
		partnerInfoResponse.setCity(city==null? "": city);
		partnerInfoResponse.setZone(zone==null? "": zone);
		partnerInfoResponse.setCompanyUuid(assistantCompanyUuid);
		partnerInfoResponse.setCompanyId(Long.parseLong(assistantCompanyId));
		partnerInfoResponse.setAgentComapnyId(Long.parseLong(agentCompanyId));
		partnerInfoResponse.setAgentCompanyUuid(agentCompanyUuid);
		partnerInfoResponse.setAgentCompanyName(agentCompanyName);
		partnerInfoResponse.setEnableWeappMsg(enableWeappMsg);
		partnerInfoResponse.setCreateTime(createTime);
		partnerInfoResponse.setCompanyName(companyName);
		partnerInfoResponse.setCompanyAbbreviation(companyAbbreviation);
		
		GetParentDeptReqDto getParentDeptReqDto = new GetParentDeptReqDto();
		if(StringUtils.isNotBlank(deptId) && !"0".equals(deptId)) {
			getParentDeptReqDto.setDeptId(Long.parseLong(deptId));
			getParentDeptReqDto.setMerchantId(Long.parseLong(agentCompanyId));
			partnerInfoResponse.setDeptId(Long.parseLong(deptId));
			GetParentDeptResDto getParentDeptResDto = organizationService.getParentDeptBySubDept(getParentDeptReqDto);
			if(getParentDeptResDto != null) {
				partnerInfoResponse.setAgentStoreId(String.valueOf(getParentDeptResDto.getStoreId()));
				partnerInfoResponse.setAgentGroupId(String.valueOf(getParentDeptResDto.getGroupId()));
			}
		}
		return partnerInfoResponse;
	}
	
	/**
	 * 分销小程序获取对应的openId信息
	 * @param jsCode
	 * @param systemId
	 * @param companyUuid
	 * @return
	 */
	public JSONObject getUserOpenIdInfo(String jsCode, String systemId, String companyUuid) {
		JSONObject wxBindInfoJson;
		if(!StringUtil.isNotNull(jsCode, systemId, companyUuid)) {
			LOGGER.info("LoginController->getUserOpenIdInfo入参非法");
			return null;
		}
		Subscription subscription = weAppUtils.getRealAppIdAndSecret(systemId, companyUuid);
		if(subscription != null) {
			wxBindInfoJson = WeAppUtils.getWxBindByJsCode(jsCode, subscription.getAppId(), subscription.getAppSecret());
		}else {
			wxBindInfoJson = WeAppUtils.getWxBindByJsCode(jsCode, "", "");
		}
		return wxBindInfoJson;
	}
	
	@ApiOperation(value="根据公司ID查询公司名称", response = PartnerRegesiterModelResponse.class)
	@PostMapping("/getCompanyNameById")
	public BaseResponse<String> getCompanyNameById(@RequestBody @Valid BaseRequest<GenericIdRequest> req) {
		Long id = req.getData().getId();
		Company company = companyService.getCompanyById(id);
		BaseResponse<String> res = new BaseResponse<>();
		if(company != null) {
			res.setData(company.getCompanyName());
			res.setCode(ResultEnum.SUCCESS.getNo());
			return res;
		}
		res.setCode(ResultEnum.FAIL.getNo());
		res.setMsg("公司邀请码错误，请查证！");
		return res;
	}
	
	@SuppressWarnings("unchecked")
	@ApiOperation(value="获取省份编码", response = AreaListResponse.class)
	@PostMapping("/getProvince")
	public BaseResponse<AreaListResponse> getProvince(@RequestBody @Valid CommonReqDTO req) {
		Object object = RedisUtil.get(RedisKeyConstant.Common.PROVINCE);
		List<Area> areaList = null;
		if(object != null) {
			areaList = (List<Area>) object;
		} else {
			areaList = areaService2.getProvince().getAreaList();
			RedisUtil.set(RedisKeyConstant.Common.PROVINCE, areaList);
		}
		AreaListResponse areaListResponse = new AreaListResponse();
		areaListResponse.setAreaResponseList(AreaResponse.listConvertAreaToAreaResponse(areaList));
		
		BaseResponse<AreaListResponse> res = new BaseResponse<>();
		res.setData(areaListResponse);
		res.setCode(ResultEnum.SUCCESS.getNo());
		return res;
	}
	
	/**
	 * 	根据父ID获取子集的area节点
	 * @param resDto
	 */
	@SuppressWarnings("unchecked")
	@ApiOperation(value="根据父ID获取子区域集合", response = AreaListResponse.class)
	@PostMapping("/getAreaByParent")
	public BaseResponse<AreaListResponse> getAreaByParent(@RequestBody @Valid BaseRequest<GenericIdRequest> req) {
		Long id = req.getData().getId();
		
		Object object = RedisUtil.get(RedisKeyConstant.Common.AREA + id);
		List<Area> areaList = null;
		if(object != null) {
			areaList = (List<Area>) object;
		} else {
			GetCityReqDto getCityReqDto = new GetCityReqDto();
			getCityReqDto.setAreaId(id);
			areaList = areaService2.getCity(getCityReqDto).getAreaList();
			RedisUtil.set(RedisKeyConstant.Common.AREA + id, areaList);
		}
		
		AreaListResponse areaListResponse = new AreaListResponse();
		areaListResponse.setAreaResponseList(AreaResponse.listConvertAreaToAreaResponse(areaList));
		BaseResponse<AreaListResponse> res = new BaseResponse<>();
		res.setData(areaListResponse);
		res.setCode(ResultEnum.SUCCESS.getNo());
		return res;
	}
	
	/**
	 * 	根据父ID获取子集的area节点
	 * @param resDto
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	@ApiOperation(value="分销公司注册", response = CommonResponse.class)
	@PostMapping("/agentCompanyRegesit")
	@Transactional(rollbackFor = Exception.class)
	public BaseResponse<AttetionResDto> agentCompanyRegesit(@RequestBody @Valid BaseRequest<AgentCompanyRegesitReqDto> req) throws Exception {
		
		BaseResponse<AttetionResDto> res = new BaseResponse<>();
		AgentCompanyRegesitReqDto agentCompanyRegesitReqDto = req.getData();
		Long devCompanyId = agentCompanyRegesitReqDto.getId();
		Company company = companyService.getCompanyById(devCompanyId);
		if(company == null) {
			res.setCode(ResultEnum.FAIL.getNo());
			res.setMsg("机构邀请码有误，请确认！");
			return res;
		}
		
		if(!"1".equals(company.getEnableAgencyRegesit())) {
			res.setCode(ResultEnum.FAIL.getNo());
			res.setMsg("该商户不支持机构自主申请注册，请联系商户对接人开通系统!");
			return res;
		}
		
		//  验证短信验证码
		boolean validateResult = LoginFactory.getLoginService(req.getLoginType()).validateCode(4, 
				agentCompanyRegesitReqDto.getAdminPhone(), req.getLoginType(), agentCompanyRegesitReqDto.getValidateCode());
		if(!validateResult) {
			res.setCode(ResultEnum.FAIL.getNo());
			res.setMsg("短信验证码输入有误，请确认!");
			return res;
		}
		
		// 1、新增待审核状态公司
		AddAgentDeptReqDto addAgentDeptReqDto = new AddAgentDeptReqDto();
		addAgentDeptReqDto.setCompanyName(agentCompanyRegesitReqDto.getCompanyName());
		addAgentDeptReqDto.setProvince(agentCompanyRegesitReqDto.getProvince());
		addAgentDeptReqDto.setCity(agentCompanyRegesitReqDto.getArea());
		addAgentDeptReqDto.setAddress(agentCompanyRegesitReqDto.getDetailAddress());
		addAgentDeptReqDto.setStatus(CheckStatusEnum.TODO.getNo());
		addAgentDeptReqDto.setChargeMan(agentCompanyRegesitReqDto.getAdminName());
		addAgentDeptReqDto.setPhone(agentCompanyRegesitReqDto.getAdminPhone());
		addAgentDeptReqDto.setCompanyId(agentCompanyRegesitReqDto.getId());
		List<ModelReqDto> modelReqDtoList = agentCompanyRegesitReqDto.getModelReqDtoList();
		List<ModelInfo> modelInfoList = new ArrayList<>();
		for (ModelReqDto modelReqDto : modelReqDtoList) {
			modelInfoList.add(modelReqDto.convertModelReqDtoToModelInfo());
		}
		addAgentDeptReqDto.setModelInfoList(modelInfoList);
		AddAgentDeptResDto addAgentDeptResDto =organizationService.addAgentDept(addAgentDeptReqDto);
		if(ResultEnum.FAIL.getNo().equals(addAgentDeptResDto.getResult())) {
			res.setCode(ResultEnum.FAIL.getNo());
			res.setMsg(addAgentDeptResDto.getResultMsg());
			return res;
		}
		
		// 获取注册的公司ID
		CompanyDto companyDto = addAgentDeptResDto.getCompany();
		Long deptId = addAgentDeptResDto.getDeptId();
		
		// 新增管理员,如果校验异常，那么需要回滚。抛出异常
		AddPartnerReqDto addPartnerReqDto = new AddPartnerReqDto();
		addPartnerReqDto.setDeptId(deptId);
		addPartnerReqDto.setAccountName(agentCompanyRegesitReqDto.getAdminName());
		addPartnerReqDto.setPhone(agentCompanyRegesitReqDto.getAdminPhone());
		addPartnerReqDto.setPassword(agentCompanyRegesitReqDto.getAdminPwd());
		addPartnerReqDto.setRePassword(agentCompanyRegesitReqDto.getReAdminPwd());
		addPartnerReqDto.setUserRoleId("");
		addPartnerReqDto.setAccountType(MechanismAccountTypeEnum.mechanism.getNo());
		addPartnerReqDto.setStatus(PartnerStatusEnum.VALID.getNo());
		addPartnerReqDto.setCheckStatus(CheckStatusEnum.TODO.getNo());
		addPartnerReqDto.setCompanyUuid(company.getCompanyUuid());
		AddPartnerResDto addPartnerResDto = personnelService.addPartner(addPartnerReqDto);
		if(ResultEnum.FAIL.getNo().equals(addPartnerResDto.getResult())) {
			// 此处不能直接返回，不然公司数据无法回滚。只相当于管理员未申请成功。但是公司却成功了
			TServerException exception = new TServerException(ResultEnum.FAIL.getNo(), addPartnerResDto.getResultMsg());
			throw exception;
		}
		
		// 更新公司的创建人ID, 创建人必须为新建的管理员账户的ID。此处需要保留给审核时使用
		Company updateCompany = new Company();
		updateCompany.setId(companyDto.getId());
		updateCompany.setCreateUserId(addPartnerReqDto.getId());
		updateCompany.setCity(companyDto.getCity());
		updateCompany.setChargeMan(companyDto.getChargeMan());
		updateCompany.setPhone(companyDto.getPhone());
		companyService.edit(updateCompany);
		
		String jsCode = agentCompanyRegesitReqDto.getJsCode();
		// 获取wxopenId信息
		if(StringUtil.isNotNull(jsCode)) {
			setOpenId(jsCode, req.getLoginType(), company.getCompanyUuid(), String.valueOf(addPartnerReqDto.getId()), "1");
			// 2、 获取关注信息
			AttetionInfo attetionInfo = homeService.getAttetionInfo(req.getLoginType(),
					String.valueOf(addPartnerReqDto.getId()), company.getCompanyUuid());
			AttetionResDto attetionResDto = new AttetionResDto();
			res.setData(attetionResDto);
			BeanUtils.copyProperties(attetionResDto, attetionInfo);
		}
		
		res.setCode(ResultEnum.SUCCESS.getNo());
		return res;
	}

	/**
	 * 	根据公司ID获取注册信息以及电脑端网址信息
	 * @param resDto
	 */
	@ApiOperation(value="根据ID获取公司注册信息", response = CompanyRegesitInfoResponse.class)
	@PostMapping("/getCompanyRegesitInfo")
	public BaseResponse<CompanyRegesitInfoResponse> getCompanyRegesitInfo(@RequestBody @Valid BaseRequest<GenericIdRequest> req) {
		
		Long id = req.getData().getId();
		Company company = companyService.getCompanyById(id);
		String adminName = company.getChargeMan();
		String adminPhone = company.getPhone();
		String companyName = company.getCompanyName();
		
		BaseResponse<CompanyRegesitInfoResponse> res = new BaseResponse<>();
		CompanyRegesitInfoResponse companyRegesitInfoResponse = new CompanyRegesitInfoResponse();
		companyRegesitInfoResponse.setAdminName(adminName);
		companyRegesitInfoResponse.setAdminPhone(adminPhone);
		companyRegesitInfoResponse.setCompanyName(companyName);
		
		CompanyDto companyDto = new CompanyDto();
		companyDto.setId(company.getId());
		List<Company> devCompany = companyMapper.getDevCompanyByAgentCompanyCode(companyDto);
		if(!CollectionUtils.isEmpty(devCompany)) {
			// 获取总代公司的companyUuid
			String agencyUrl = h5UrlConfig.getAgencyWebUrl() + AGENCY_WEBURL.replace(COMPANY_UUID, 
					devCompany.get(0).getCompanyUuid());
			companyRegesitInfoResponse.setAgencyUrl(agencyUrl);
		}
		res.setData(companyRegesitInfoResponse);
		res.setCode(ResultEnum.SUCCESS.getNo());
		return res;
	}
	
	@ApiOperation(value="公司编码获取公司ID", response = GenericLongResponse.class)
	@PostMapping("/getCompanyIdByCompanyCode")
	public BaseResponse<GenericLongResponse> getCompanyIdByCompanyCode(@RequestBody @Valid BaseRequest<GenericStringRequest> req) {
		BaseResponse<GenericLongResponse> res = new BaseResponse<>();
		
		String companyCode = req.getData().getData();
		//只能查审核通过的
		CompanyDto companyDto = new CompanyDto();
		companyDto.setCompanyCode(companyCode);
		companyDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
		companyDto.setCompanyType(1L);
		companyDto.setStatus(CheckStatusEnum.SUCCESS.getNo());
		Company company = companyService.selectByDto(companyDto);
		if(company == null) {
			res.setCode(ResultEnum.FAIL.getNo());
			res.setMsg("机构码有误，请查证后重新输入");
			return res;
		}
		GenericLongResponse genericLongResponse = new GenericLongResponse();
		genericLongResponse.setData(company.getId());
		res.setData(genericLongResponse);
		return res;
	}
}
